﻿using Painter.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace Painter.Component
{
    class GroupScroll : ScrollViewer
    {
        public new IScrollInfo ScrollInfo
        {
            get { return base.ScrollInfo; }
        }
        public GroupScroll()
        {
            HorizontalAlignment = HorizontalAlignment.Left;
            VerticalAlignment = VerticalAlignment.Top;
            ScrollChanged += GroupScroll_ScrollChanged;
        }
        private void GroupScroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
           
        }
        protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                ScaleData scale_data = ScaleData.Find_CreateScaleData(this);
                if (Content is Grid)
                {
                    if (scale_data.Scale >= 1)
                    {
                        (Content as Grid).RenderTransform = new ScaleTransform() { ScaleX = 1, ScaleY = 1 };
                        (Content as Grid).Width = scale_data.BaseWidth * scale_data.Scale;
                        (Content as Grid).Height = scale_data.BaseHeight * scale_data.Scale;
                    }
                    else
                    {
                        (Content as Grid).RenderTransform = new ScaleTransform() { ScaleX = scale_data.Scale, ScaleY = scale_data.Scale };
                    }

                    (Content as Grid).Children.Cast<FrameworkElement>().ToList().ForEach(o =>
                    {
                        if (o != null && o is InkCanvas && scale_data.Scale >= 1)
                            o.RenderTransform = new ScaleTransform() { ScaleX = scale_data.Scale, ScaleY = scale_data.Scale };

                        if (o != null && o is Grid)
                        {
                            if (scale_data.Scale >= 1)
                            {
                                o.RenderTransform = new ScaleTransform() { ScaleX = 1, ScaleY = 1 };
                                o.Width = scale_data.BaseWidth * scale_data.Scale;
                                o.Height = scale_data.BaseHeight * scale_data.Scale;
                            }
                        }
                        if (o != null && o is ScaleGrid) (o as ScaleGrid).ScaleBox = (Parent as ScaleBorder).Interval < 20 ? scale_data.Scale : 10;
                    });
                }
                ScrollToVerticalOffset(ScrollInfo.VerticalOffset);
                Console.Out.WriteLine("wheel " + ScrollInfo.VerticalOffset);
            }
        }
        protected override Size MeasureOverride(Size arrangeSize)
        {
            if (Content is FrameworkElement)
            {
                ScaleData scale_data = ScaleData.Find_CreateScaleData(this);
                Thickness margin = (Content as FrameworkElement)?.Margin ?? new Thickness(0);
                if (double.IsNaN((Content as FrameworkElement).Width))
                    (Content as FrameworkElement).Width = scale_data.BaseWidth;
                if (double.IsNaN((Content as FrameworkElement).Height))
                    (Content as FrameworkElement).Height = scale_data.BaseHeight;
            }
            return base.MeasureOverride(arrangeSize);
        }
    }
}
